home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / MacButtonBorder.java < prev    next >
Text File  |  1998-06-30  |  7KB  |  217 lines

  1. /*
  2.  * @(#)MacButtonBorder.java    1.5 98/02/02
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.mac;
  22.  
  23. import com.sun.java.swing.*;
  24. import com.sun.java.swing.border.*;
  25. import com.sun.java.swing.plaf.*;
  26.  
  27. import java.awt.Component;
  28. import java.awt.Insets;
  29. import java.awt.Color;
  30. import java.awt.Graphics;
  31. import java.awt.Image;
  32. import java.awt.Point;
  33. import java.awt.Dimension;
  34. import java.util.Vector;
  35.  
  36. /**
  37.  * @version @(#)MacButtonBorder.java    1.0 12/5/97
  38.  * @author Symantec
  39.  * @author Levi Brown
  40.  */
  41.  
  42. class MacButtonBorder extends AbstractBorder implements UIResource
  43. {
  44.     final static Insets borderInsets            = new Insets(3, 3, 3, 3);
  45.     final static Insets defaultBorderInsets    = new Insets(3, 3, 3, 3);
  46.  
  47.     protected static Color GSBColor    = UIManager.getColor("GrayscaleAppearanceB");
  48.     protected static Color GSWColor    = UIManager.getColor("GrayscaleAppearanceW");
  49.     protected static Color GS2Color    = UIManager.getColor("GrayscaleAppearance2");
  50.     protected static Color GS4Color    = UIManager.getColor("GrayscaleAppearance4");
  51.     protected static Color GS5Color    = UIManager.getColor("GrayscaleAppearance5");
  52.     protected static Color GS7Color    = UIManager.getColor("GrayscaleAppearance7");
  53.     protected static Color GS8Color    = UIManager.getColor("GrayscaleAppearance8");
  54.     protected static Color GS9Color    = UIManager.getColor("GrayscaleAppearance9");
  55.     protected static Color GS10Color    = UIManager.getColor("GrayscaleAppearance10");
  56.     protected static Color GS11Color    = UIManager.getColor("GrayscaleAppearance11");
  57.  
  58.     protected static ImageIcon upPB        = null;
  59.     protected static ImageIcon downPB;
  60.     protected static ImageIcon disabledPB;
  61.     protected static ImageIcon defaultEnabledPB;
  62.     protected static ImageIcon defaultDisabledPB;
  63.  
  64.     public MacButtonBorder()
  65.     {
  66.         if(upPB == null)
  67.         {
  68.             upPB                = (ImageIcon) UIManager.getIcon("PushButton.up");
  69.             downPB                = (ImageIcon) UIManager.getIcon("PushButton.down");
  70.             disabledPB            = (ImageIcon) UIManager.getIcon("PushButton.disabled");
  71.             defaultEnabledPB    = (ImageIcon) UIManager.getIcon("PushButton.defaultEnabled");
  72.             defaultDisabledPB    = (ImageIcon) UIManager.getIcon("PushButton.defaultDisabled");
  73.         }
  74.     }
  75.     
  76.     public void paintBorder(Component c, Graphics g, int x, int y, int width, int height)
  77.     {
  78.         boolean    isPressed        = false;
  79.         boolean    isEnabled        = true;
  80.         boolean    isDefault        = false;
  81.         boolean    isActive        = true;
  82.         int            internalWidth    = width;
  83.         int            internalHeight    = height;
  84.         int            xOffset            = 0;
  85.         int            yOffset            = 0;
  86.         ImageIcon    workingIcon;
  87.         Vector        colors;
  88.         
  89.         if (c instanceof AbstractButton)
  90.         {
  91.             AbstractButton b = (AbstractButton)c;
  92.             ButtonModel model = b.getModel();
  93.             
  94.             isPressed = (model.isArmed() && model.isPressed());
  95.             isEnabled = model.isEnabled();
  96.             
  97.             ComponentUI ui = b.getUI();
  98.             if(ui instanceof MacButtonUI)
  99.             {
  100.                 isActive = ((MacButtonUI)ui).isActive();
  101.             }
  102.         }
  103.         
  104.         if(c instanceof JButton)
  105.         {
  106.             JButton b = (JButton)c;
  107.             isDefault = b.isDefaultButton();
  108.         }
  109.         
  110.         
  111.         if(isDefault)
  112.         {
  113.             colors = new Vector();
  114.             
  115.             if(isEnabled && isActive)
  116.             {
  117.                 workingIcon        = defaultEnabledPB;
  118.                 
  119.                 colors.addElement(GSBColor);    //Top and Left
  120.                 colors.addElement(GSBColor);    //Bottom and Right
  121.                 colors.addElement(GS2Color);    //Top and Left
  122.                 colors.addElement(GS8Color);    //Bottom and Right
  123.                 colors.addElement(GS5Color);    //Top and Left
  124.                 colors.addElement(GS5Color);    //Bottom and Right
  125.             }
  126.             //Disabled
  127.             else
  128.             {
  129.                 workingIcon        = defaultDisabledPB;
  130.     
  131.                 colors.addElement(GS7Color);    //Top and Left
  132.                 colors.addElement(GS7Color);    //Bottom and Right
  133.                 colors.addElement(GS4Color);    //Top and Left
  134.                 colors.addElement(GS4Color);    //Bottom and Right
  135.                 colors.addElement(GS4Color);    //Top and Left
  136.                 colors.addElement(GS4Color);    //Bottom and Right
  137.             }
  138.     
  139.             MacBorderFactory.paintButtonBorder(c, g, workingIcon, colors, new Point(xOffset, yOffset), new Dimension(internalWidth, internalHeight));
  140.             internalWidth    -= defaultBorderInsets.left + defaultBorderInsets.right;
  141.             internalHeight    -= defaultBorderInsets.top + defaultBorderInsets.bottom;
  142.             xOffset = defaultBorderInsets.left;
  143.             yOffset = defaultBorderInsets.top;
  144.         }
  145.  
  146.         colors = new Vector();
  147.         
  148.         if(isEnabled && isActive)
  149.         {
  150.             if(isPressed)
  151.             {
  152.                 workingIcon        = downPB;
  153.  
  154.                 colors.addElement(GSBColor);    //Top and Left
  155.                 colors.addElement(GSBColor);    //Bottom and Right
  156.                 colors.addElement(GS11Color);    //Top and Left
  157.                 colors.addElement(GS7Color);    //Bottom and Right
  158.                 colors.addElement(GS10Color);    //Top and Left
  159.                 colors.addElement(GS8Color);    //Bottom and Right
  160.             }
  161.             //If it's released
  162.             else
  163.             {
  164.                 workingIcon        = upPB;
  165.                 
  166.                 colors.addElement(GSBColor);    //Top and Left
  167.                 colors.addElement(GSBColor);    //Bottom and Right
  168.                 colors.addElement(GS2Color);    //Top and Left
  169.                 colors.addElement(GS8Color);    //Bottom and Right
  170.                 colors.addElement(GSWColor);    //Top and Left
  171.                 colors.addElement(GS5Color);    //Bottom and Right
  172.             }
  173.         }
  174.         //Disabled
  175.         else
  176.         {
  177.             workingIcon        = disabledPB;
  178.  
  179.             colors.addElement(GS7Color);    //Top and Left
  180.             colors.addElement(GS7Color);    //Bottom and Right
  181.             colors.addElement(GS2Color);    //Top and Left
  182.             colors.addElement(GS2Color);    //Bottom and Right
  183.             colors.addElement(GS2Color);    //Top and Left
  184.             colors.addElement(GS2Color);    //Bottom and Right
  185.         }
  186.         
  187.         MacBorderFactory.paintButtonBorder(c, g, workingIcon, colors, new Point(xOffset, yOffset), new Dimension(internalWidth, internalHeight));
  188.     }
  189.     
  190.     public Insets getBorderInsets(Component c)
  191.     {
  192.         boolean    isDefault    = false;
  193.         Insets        insets        = (Insets) borderInsets.clone();
  194.         
  195.         if(c instanceof JButton)
  196.         {
  197.             JButton b = (JButton)c;
  198.             isDefault = b.isDefaultButton();
  199.         }
  200.         
  201.         if(isDefault)
  202.         {
  203.             insets.top        += defaultBorderInsets.top;
  204.             insets.left        += defaultBorderInsets.left;
  205.             insets.bottom    += defaultBorderInsets.bottom;
  206.             insets.right    += defaultBorderInsets.right;
  207.         }
  208.         
  209.         return insets;
  210.     }
  211.     
  212.     public Insets getDefaultBorderInsets(Component c)
  213.     {
  214.         return defaultBorderInsets;
  215.     }    
  216. }
  217.